package org.finir.fakturisanje.rich.web.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

import org.finir.fakturisanje.business.core.GenerickiEAO;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.log.Log;

@org.jboss.seam.annotations.faces.Converter
@Name("domainConverter")
@Scope(ScopeType.PAGE)
public class DomainConverter implements Converter {

	@SuppressWarnings("unchecked")
	@In("#{generickiEAO}")
	GenerickiEAO generickiEAO;

	@Logger
	Log log;

	@SuppressWarnings("unchecked")
	@Override
	public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
		// TODO Auto-generated method stub
		log.info(arg2);
		Class type = arg1.getValueBinding("value").getType(arg0);
		log.info("class type " + type.getName());
		Long id = Long.parseLong(arg2.substring(arg2.lastIndexOf("=") + 1, arg2
				.length()));
		log.info("obj id" + id);
		return generickiEAO.get(type, id);

	}

	@Override
	public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
		// TODO Auto-generated method stub

		return arg2.toString();
	}

}
